package com.cwnuzj.ffms;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.cwnuzj.ffms.util.CacheImage;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	private IWorkbenchWindow window;
	private Shell shell;
	private TrayItem trayItem;
	private Image trayImage;
	
	private final static String HELP_COMMAND_ID = "org.eclipse.ui.help.helpContents";
	private final static String ABOUT_COMMAND_ID = "org.eclipse.ui.help.aboutAction";
	private final static String EXIT_COMMAND_ID = "org.eclipse.ui.file.exit";

	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(1000, 700));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setShowPerspectiveBar(true);
	}

	@Override
	public void postWindowOpen() {
		super.postWindowOpen();
		window = getWindowConfigurer().getWindow();
		shell = window.getShell();
		trayItem = initTaskItem(window);
		// Some OS might not support tray items
		if (trayItem != null) {
			minimizeBehavior();
			// Create exit and about action on the icon
			hookPopupMenu();
		}

		IStatusLineManager statusline = getWindowConfigurer().getActionBarConfigurer().getStatusLineManager();
		statusline.setMessage(null, "Status line is ready");
	}

	// Add a listener to the shell
	private void minimizeBehavior() {
		window.getShell().addShellListener(new ShellAdapter() {
			// If the window is minimized hide the window
			public void shellIconified(ShellEvent e) {
				window.getShell().setVisible(false);
			}
		});
		// If user double-clicks on the tray icons the application will be
		// visible again
		trayItem.addListener(SWT.DefaultSelection, new Listener() {
			public void handleEvent(Event event) {

				if (!shell.isVisible()) {
					window.getShell().setMinimized(false);
					shell.setVisible(true);
				} else {
					window.getShell().setMinimized(true);
					shell.setVisible(false);
				}
			}
		});
	}

	// We hook up on menu entry which allows to close the application
	private void hookPopupMenu() {
		trayItem.addListener(SWT.MenuDetect, new Listener() {
			public void handleEvent(Event event) {
				Menu menu = new Menu(window.getShell(), SWT.POP_UP);

				// Creates a new menu item that terminates the program
				// when selected
				final MenuItem open = new MenuItem(menu, SWT.NONE);
				if (!shell.isVisible()) {
					open.setText("打开主界面");
				} else {
					open.setText("隐藏主界面");
				}
				open.setImage(CacheImage.getINSTANCE().getImage("icons/application.png"));
				open.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						if (!shell.isVisible()) {
							window.getShell().setMinimized(false);
							shell.setVisible(true);
						} else {
							window.getShell().setMinimized(true);
							shell.setVisible(false);
						}
					}
				});

				MenuItem help = new MenuItem(menu, SWT.NONE);
				help.setText("打开帮助");
				help.setImage(CacheImage.getINSTANCE().getImage("icons/help.png"));
				help.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
						try {
							handlerService.executeCommand(HELP_COMMAND_ID, null);
						} catch (Exception ex) {
							throw new RuntimeException(HELP_COMMAND_ID);
						}
					}
				});

				MenuItem exit = new MenuItem(menu, SWT.NONE);
				exit.setText("退出");
				exit.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						// Lets call our command
						IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
						try {
							handlerService.executeCommand(EXIT_COMMAND_ID, null);
						} catch (Exception ex) {
							throw new RuntimeException(EXIT_COMMAND_ID);
						}
					}
				});

				MenuItem about = new MenuItem(menu, SWT.NONE);
				about.setText("关于");
				about.setImage(CacheImage.getINSTANCE().getImage("icons/caiwu/16x16/coins.png"));
				about.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						// Lets call our command
						IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
						try {
							handlerService.executeCommand(ABOUT_COMMAND_ID, null);
						} catch (Exception ex) {
							throw new RuntimeException(ABOUT_COMMAND_ID);
						}
					}
				});
				// We need to make the menu visible
				menu.setVisible(true);
			}
		});
	}

	// This methods create the tray item and return a reference
	private TrayItem initTaskItem(IWorkbenchWindow window) {
		final Tray tray = window.getShell().getDisplay().getSystemTray();
		TrayItem trayItem = new TrayItem(tray, SWT.NONE);

		trayImage = CacheImage.getINSTANCE().getImage("icons/caiwu/16x16/coins.png");
		trayItem.setImage(trayImage);
		trayItem.setToolTipText("TrayItem");
		return trayItem;

	}

	// We need to clean-up after ourself
	@Override
	public void dispose() {
		if (trayImage != null) {
			trayImage.dispose();
		}
		if (trayItem != null) {
			trayItem.dispose();
		}
	}
}
